/*//地址待定
function appUrl(url) {
    let http ="http://localhost:8080/blog_api/";
    return http+url;
}*/

//本地测试地址
function appUrl(url) {
    let http = "http://localhost:8080/blog_api/";
    return http + url;
}

/**
 * 对返回结果进行处理
 *
 * @param json
 * @param callback
 */
function handlerRead(json, callback) {
    let parse = JSON.parse(json);

    if (parse.code === 100) {
        //成功执行回调函数
        callback(parse.data);
    } else {
        alert(parse.msg);
    }
}

/**
 * doGet请求无参
 * @param url
 * @param callback
 */
function doGet(url, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "get",
        url: url1,
        success: function (resp) {
            //这里可能有点绕，仔细看
            handlerRead(resp, callback);
        }
    });
}

/**
 * post请求带参
 * @param url
 * @param callback
 * @param json
 */
function doPost(url, json, callback) {
    let json1 = JSON.stringify(json);
    let url1 = appUrl(url);
    $.ajax({
        type: "post",
        url: url1,
        data: json1,
        success: function (resp) {
            handlerRead(resp, callback);
        }
    });
}

/**
 * post请求带参(不转json)
 * @param url
 * @param callback
 * @param json
 */
function doCasualPost(url, callback, params) {
    let url1 = appUrl(url);
    $.ajax({
        type: "post",
        url: url1,
        data: params,
        success: function (resp) {
            handlerRead(resp, callback)
        }
    });
}

/**
 * get请求带参 模糊搜索文章无需用json语句（仅陈国源用,石建朋也要用）
 * @param url
 * @param callback
 * @param json
 */
function getWithParams(url, params, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "get",
        url: url1,
        data: params,
        success: function (resp) {
            handlerRead(resp, callback)
        }
    })
}

/**
 * 不转json-post请求
 * @param url
 * @param params
 * @param callback
 */
function postWithoutJson(url, params, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "post",
        url: url1,
        data: params,
        success: function (resp) {
            handlerRead(resp, callback)
        }
    });
}

/**
 * 不需要判断是否成功
 * @param url
 * @param callback
 */
function doGet1(url, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "get",
        url: url1,
        success: function (resp) {
            console.log(resp)

        }
    });
}
